package com.gxust.controller;

import com.gxust.pojo.PageBean;
import com.gxust.pojo.Product;
import com.gxust.util.jwt.GetData;
import com.gxust.util.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 商品管理
 */
@Slf4j
@RestController
@Api(tags = "商品管理")
public class ProductController {
    @Autowired
    private com.gxust.service.ProductService ProductService;

    /**
     * 添加商品
     */
    @PostMapping("/product/add")
    @ApiOperation("新增商品信息")
    public Result add(@RequestBody Product product,HttpServletRequest request){
        log.info("新增商品信息：{}",product);
        Integer userId = GetData.getUserId(request);
        ProductService.add(product,userId);
        return Result.success();
    }

    /**
     * 删除商品
     */
    @PostMapping("/product/delete")
    @ApiOperation("删除商品信息")
    public Result del(@RequestBody Product product){
        log.info("删除商品信息: {}",product);
        ProductService.del(product);
        return Result.success();
    }

    /**
     * 修改商品信息
     */
    @PostMapping("/product/update")
    @ApiOperation("修改商品信息")
    public Result update(@RequestBody Product product){
        log.info("修改商品信息: {}",product);
        ProductService.update(product);
        return Result.success();
    }

    /**
     * 分页查询
     */
    @GetMapping("/product/info")
    @ApiOperation("获取商品列表")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       HttpServletRequest request){
        log.info("分页查询, 参数: {},{}",page,pageSize);
        //调用service分页查询
        Integer userId = GetData.getUserId(request);
        PageBean pageBean = ProductService.page(page,pageSize,userId);
        return Result.success(pageBean);
    }

    /**
     * 商城首页列表
     */
    @GetMapping("/mall/index")
    @ApiOperation("商城首页列表")
    public Result MallIndex(){
        log.info("商品首页");
//        调用service函数
        Object productList = ProductService.mallList();
        return  Result.success(productList);
    }

    @GetMapping("/mall/indexSelect")
    @ApiOperation("商城首页列表")
    public Result MallIndexSelect(){
        log.info("商城首页列表");
//        调用service函数
        Object productList = ProductService.mallListSelect();
        return  Result.success(productList);
    }


    /**
     * 按id查商品
     */
    @PostMapping("/mall/product_info")
    @ApiOperation("查询商品详细信息")
    public Result ProductInfoById(@RequestBody Map<String, Object> request) {
        String productIdStr = (String) request.get("productId");
        Integer productId = Integer.parseInt(productIdStr);
        log.info("查询商品详细信息");
        // 调用 service 函数
        Product productInfo = ProductService.ProductInfoById(productId);
        return Result.success(productInfo);
    }

    @GetMapping("/productBySellerId")
    @ApiOperation("根据商家id查询商品")
    public Result ProductBySellerId(Integer id){
//        id= BaseContext.getCurrentId();
        List<Product> list = ProductService.ProductBySellerId(id);
        return Result.success(list);
    }

}

